#include "queue.h"


void QInit(queue* q)
{
    q->head = NULL;
    q->tail = NULL;
}

void QPush(queue*q, DataType value)
{
    assert(q);
    QNode* s = (QNode*) malloc(sizeof(QNode));
    if(s == NULL)
    {
        printf("malloc fail\n");
        exit(-1);
    }
    s->data = value;
    s->next = NULL;

    if(q->tail == NULL)
    {
        q->head = s;
        q->tail = s;
    }
    else
    {
        q->tail->next = s;
        q->tail = s;
    }
}

void QPrint(queue* q)
{
    assert(q->head);
    printf("队列：");
    QNode*H = q->head;
    while(H != NULL)
    {
        printf("%d ", H->data);
        H = H->next;
    }
}

void QPop(queue* q)
{
    assert(q);
    assert(q->head);
    
    if(q->head->next == NULL)
    {
        free(q->head);
        q->tail = NULL;
        q->head = NULL;
    }
    else
    {
        QNode* H = q->head->next;
        free(q->head);     
        q->head = H; 
    } 
    
}


void Destroyed(queue* q)
{
    assert(q);
    assert(q->head);
    if(q->head->next == NULL)
    {
        free(q->head);
        q->head = q->tail = NULL;
    }
    else
    {
        QNode* H = q->head;
        while(H != NULL)
        {
            H = H->next;
            free(q->head);
            q->head = H;
        }
        q->head = NULL;
        q->tail = NULL;
    }
}

//返回队头元素
int QGet(queue* q)
{
    return q->head->data;
}

int empty(queue* q)
{
    return q->head == NULL? 1 : 0;
}
